#include "font_regen.h"
#include <native_drawing/drawing_color.h>
#include <native_drawing/drawing_brush.h>
#include <native_drawing/drawing_matrix.h>
#include <native_drawing/drawing_path.h>
#include <native_drawing/drawing_pen.h>
#include <native_drawing/drawing_font.h>
#include <native_drawing/drawing_text_blob.h>
#include "test_common.h"
#include "common/log_common.h"
#include <array>

enum{
    WIDTH = 512,
    HEIGHT = 512,
    N = 10
};
OH_Drawing_TextBlob* make_blob(const std::string& text , OH_Drawing_Font* font)
{
    size_t len = text.length();
    std::array<float,N> pos;
    OH_Drawing_TextBlobBuilder* builder = OH_Drawing_TextBlobBuilderCreate();
    OH_Drawing_Rect* rect = OH_Drawing_RectCreate(0, 0, WIDTH, HEIGHT);
    
    int count =  OH_Drawing_FontCountText(font,text.c_str(),len,OH_Drawing_TextEncoding::TEXT_ENCODING_UTF8);
    auto buffer = OH_Drawing_TextBlobBuilderAllocRunPos(builder,font,count,rect);
    memcpy(buffer->pos,pos.begin(),len);
    return OH_Drawing_TextBlobBuilderMake(builder);
}

BadApple::BadApple() {
    //skia dm file gm/fontregen.cpp
    bitmapWidth_ = WIDTH;
    bitmapHeight_ = HEIGHT;
    fileName_ = "badapple";
}


void BadApple::OnTestFunction(OH_Drawing_Canvas* canvas)
{
    static const std::string kTexts[] = {
        ("Meet"),
        ("iPad Pro"),
    };
    OH_Drawing_Font* font = OH_Drawing_FontCreate();
    OH_Drawing_FontSetTextSize(font, 256);
    OH_Drawing_TextBlob* fBlobs[3];
    fBlobs[0] = make_blob(kTexts[0], font);
    fBlobs[1] = make_blob(kTexts[1], font);

    OH_Drawing_Pen *pen = OH_Drawing_PenCreate();
    OH_Drawing_PenSetColor(pen, 0xFF111111);
    OH_Drawing_CanvasAttachPen(canvas, pen);
    
    OH_Drawing_CanvasDrawTextBlob(canvas, fBlobs[0], 10, 260);
    OH_Drawing_CanvasDrawTextBlob(canvas, fBlobs[1], 10, 500);
}
